package top.yfq.shortlinkweb.util;

/**
 * @author yfq
 * @date 2025/7/18 16:24
 * @description
 */
public class Base62Encoder {
    private Base62Encoder() {
    }

    // 偏移量，保证生成的短码最少有6位
    private static final long OFFSET = (long) Math.pow(62, 5);
    // 打乱后的字符集，避免生成的代码带顺序
    private static final String BASE62 = "fzC4YgDFxShTv8pHPVdEO3GWABrQkNZ1XotLlcUI6nJuaM950eq2bsRm7Kwjiy";

    /**
     * ID映射为8位短码
     */
    public static String encode(long id) {
        id = id + OFFSET;
        StringBuilder sb = new StringBuilder();
        while (id > 0) {
            sb.append(BASE62.charAt((int) (id % 62)));
            id /= 62;
        }
        return sb.reverse().toString();
    }
}
